package a;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-26
 * Time: 22:51
 */
public class TcpEchoClient {
    Socket socket = null;
    public TcpEchoClient(String serveIP,int servePort) throws IOException {
     socket = new Socket(serveIP,servePort);
    }
    public void start(){
        System.out.println("客户端启动!");
        Scanner scanner =new Scanner(System.in);
        try(InputStream inputStream =socket.getInputStream();
            OutputStream outputStream =socket.getOutputStream()) {

            Scanner scanner1 = new Scanner(socket.getInputStream());
            while(true){
                System.out.print("请输入请求数据:");
                String request =scanner.next();
                request += "\n";
              outputStream.write(request.getBytes());
              if(!scanner1.hasNext()){
               break;
              }
              String response = scanner1.next();
                System.out.println(response);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1",9091);
        tcpEchoClient.start();

    }
}
